/**
 * Created by kevin on 17/4/6.
 */

var path = require('path')
var docker = require('./docker')
var tar = require('tar-fs')
var shell = require('shelljs')

var spawn = require('child_process').spawn;


module.exports = function(io) {
    io.on('connection', function(socket) {
        console.log(socket.id)

        // var version = shelljs.exec('docker --version', {silent:true}).stdout;
        socket.on('if_exit', function(dt) {
            shell.exec('docker --version', function(code, stdout, stderr) {
                if(code == 0) {
                    socket.emit('if_exit', {
                        code: code,
                        msg: stdout
                    })
                }else {
                    socket.emit('if_exit', {
                        code: code,
                        msg: stderr
                    })
                }
            });
        })

        socket.on('status', function(dt) {
            shell.exec('docker ps -a', function(code, stdout, stderr) {
                if(code == 0) {
                    socket.emit('status', {
                        code: code,
                        msg: '开启'
                    })
                }else {
                    socket.emit('status', {
                        code: code,
                        msg: '关闭'
                    })
                }
            });
        })


        socket.on('exec', function(dt) {
            shell.exec(dt.cmd, function(code, stdout, stderr) {
                if(code == 0) {
                    socket.emit('exec', {
                        code: code,
                        msg: stdout
                    })
                }else {
                    socket.emit('exec', {
                        code: code,
                        msg: stderr
                    })
                }
            })
        })

        socket.on('build-image', function(dt) {
            console.log(' in socket', dt);

            var name = dt.name;
            var centos7_path = path.resolve(__dirname, '../../dockerfiles/centos7');
            var tarStream = tar.pack(centos7_path)

            docker.buildImage(
                tarStream, {t: name}, function(err, response) {
                    if(err) {
                        console.log(err, 'err')
                        socket.emit('info',{
                            msg: err
                        })
                    } else {
                        // response.pipe(process.stdout)
                        response.on("data", function(data) {
                            socket.emit('info',{
                                msg: JSON.parse(data.toString()).stream
                            })
                        });
                        response.on("end", function() {
                            console.log("response ended");
                        });
                    }
                });
        })

        socket.on('list-image', function(dt) {
            console.log('list-image', dt)
            dockerImage  = spawn('docker', ['images']);

            // 捕获标准输出并将其打印到控制台
            dockerImage.stdout.on('data', function (data) {
                console.log('标准输出：\n' + data);
            });

            // 捕获标准错误输出并将其打印到控制台
            dockerImage.stderr.on('data', function (data) {
                console.log('标准错误输出：\n' + data);
            });

            // 注册子进程关闭事件
            dockerImage.on('exit', function (code, signal) {
                console.log('子进程已退出，代码：' + code);
            });
        })
    })
}